home *** CD-ROM | disk | FTP | other *** search
/ Inside Multimedia 1994 April / Inside Multimedia CD-ROM (April 1994).iso / prg / gs / gssource.exe / GDEVPRN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-24  |  10.9 KB  |  355 lines

  1. /* Copyright (C) 1990, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevprn.c */
  21. /* Generic printer support for Ghostscript */
  22. #include "gdevprn.h"
  23. #include "gp.h"
  24. #include "gsprops.h"
  25.  
  26. /* Define the scratch file name prefix for mktemp */
  27. #define SCRATCH_TEMPLATE gp_scratch_file_name_prefix
  28.  
  29. /* Internal routine for opening a scratch file */
  30. private int
  31. open_scratch(char *fname, FILE **pfile)
  32. {    char fmode[4];
  33.     strcpy(fmode, "w+");
  34.     strcat(fmode, gp_fmode_binary_suffix);
  35.     *pfile = gp_open_scratch_file(SCRATCH_TEMPLATE, fname, fmode);
  36.     if ( *pfile == NULL )
  37.        {    eprintf1("Could not open the scratch file %s.\n", fname);
  38.         return_error(gs_error_invalidfileaccess);
  39.        }
  40.     return 0;
  41. }
  42.  
  43. /* ------ Standard device procedures ------ */
  44.  
  45. /* Macros for casting the pdev argument */
  46. #define ppdev ((gx_device_printer *)pdev)
  47. #define pmemdev ((gx_device_memory *)pdev)
  48. #define pcldev ((gx_device_clist *)pdev)
  49.  
  50. gx_device_procs prn_std_procs =
  51.   prn_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close);
  52.  
  53. /* Generic initialization for the printer device. */
  54. /* Specific devices may wish to extend this. */
  55. int
  56. gdev_prn_open(gx_device *pdev)
  57. {    const gx_device_memory *mdev =
  58.       gdev_mem_device_for_bits(pdev->color_info.depth);
  59.     gx_device_procs *pprocs = pdev->procs;
  60.     ulong mem_space;
  61.     byte *base;
  62.     if ( mdev == 0 )
  63.         return_error(gs_error_rangecheck);
  64.     memset(ppdev->skip, 0, sizeof(ppdev->skip));
  65.     ppdev->orig_procs = pprocs;
  66.     ppdev->page_count = 0;
  67.     ppdev->file = ppdev->ccfile = ppdev->cbfile = NULL;
  68.     mem_space = gdev_mem_bitmap_size(pmemdev);
  69.     if ( mem_space >= ppdev->max_bitmap ||
  70.          mem_space != (uint)mem_space ||    /* too big to allocate */
  71.          (base = (byte *)gs_malloc((uint)mem_space, 1, "printer buffer")) == 0    /* can't allocate */
  72.        )
  73.        {    /* Buffer the image in a command list. */
  74.         uint space;
  75.         int code;
  76.         for ( space = ppdev->use_buffer_space; ; )
  77.            {    base = (byte *)gs_malloc(space, 1,
  78.                          "command list buffer");
  79.             if ( base != 0 ) break;
  80.             if ( (space >>= 1) < PRN_MIN_BUFFER_SPACE )
  81.                 return_error(gs_error_VMerror);    /* no hope */
  82.            }
  83. open_c:        pcldev->data = base;
  84.         pcldev->data_size = space;
  85.         pcldev->target = pdev;
  86.         pcldev->mdev = *mdev;
  87.         ppdev->buf = base;
  88.         ppdev->buffer_space = space;
  89.         /* Try opening the command list, to see if we allocated */
  90.         /* enough buffer space. */
  91.         code = (*gs_clist_device.procs->open_device)((gx_device *)pcldev);
  92.         if ( code < 0 )
  93.         {    /* If there wasn't enough room, and we haven't */
  94.             /* already shrunk the buffer, try enlarging it. */
  95.             if ( code == gs_error_limitcheck &&
  96.                  space >= ppdev->use_buffer_space
  97.                )
  98.             {    gs_free((char *)base, space, 1,
  99.                     "command list buffer");
  100.                 space <<= 1;
  101.                 base = (byte *)gs_malloc(space, 1,
  102.                          "command list buffer");
  103.                 ppdev->buf = base;
  104.                 if ( base != 0 ) goto open_c;
  105.             }
  106.             return code;
  107.         }
  108.         if ( (code = open_scratch(ppdev->ccfname, &ppdev->ccfile)) < 0 )
  109.             return code;
  110.         if ( (code = open_scratch(ppdev->cbfname, &ppdev->cbfile)) < 0 )
  111.             return code;
  112.         pcldev->cfile = ppdev->ccfile;
  113.         pcldev->bfile = ppdev->cbfile;
  114.         pcldev->bfile_end_pos = 0;
  115.         ppdev->mod_procs = *gs_clist_device.procs;
  116.        }
  117.     else
  118.        {    /* Render entirely in memory. */
  119.         ppdev->buffer_space = 0;
  120.         ppdev->ccfile = NULL;
  121.         ppdev->cbfile = NULL;
  122.         pmemdev->base = base;
  123.         ppdev->mod_procs = *mdev->procs;
  124.        }
  125.     /* Synthesize the procedure vector. */
  126.     /* Rendering operations come from the memory or clist device, */
  127.     /* non-rendering come from the printer device. */
  128.     pdev->procs = &ppdev->mod_procs;
  129. #define copy_proc(p) ppdev->mod_procs.p = pprocs->p
  130.     copy_proc(get_initial_matrix);
  131.     copy_proc(output_page);
  132.     copy_proc(close_device);
  133.     copy_proc(map_rgb_color);
  134.     copy_proc(map_color_rgb);
  135.     copy_proc(get_props);
  136.     copy_proc(put_props);
  137. #undef copy_proc
  138.     return (*pdev->procs->open_device)(pdev);
  139. }
  140.  
  141. /* Added properties for printers */
  142.  
  143. private const gs_prop_item props_prn[] = {
  144.     prop_def("BufferSpace", prt_int),
  145.     prop_def("MaxBitmap", prt_int),
  146.     prop_def("OutputFile", prt_string)
  147. };
  148.  
  149. /* Get properties.  In addition to the standard properties, */
  150. /* we supply the max bitmap size, buffer size, and output name. */
  151. int
  152. gdev_prn_get_props(gx_device *pdev, gs_prop_item *plist)
  153. {    int start = gx_default_get_props(pdev, plist);
  154.     if ( plist != 0 )
  155.        {    register gs_prop_item *pi = plist + start;
  156.         memcpy(pi, props_prn, sizeof(props_prn));
  157.         pi[0].value.i = ppdev->use_buffer_space;
  158.         pi[1].value.i = ppdev->max_bitmap;
  159.         pi[2].value.a.p.s = ppdev->fname;
  160.         pi[2].value.a.size = -1;
  161.        }
  162.     return start + sizeof(props_prn) / sizeof(gs_prop_item);
  163. }
  164.  
  165. /* Put properties. */
  166. int
  167. gdev_prn_put_props(gx_device *pdev, gs_prop_item *plist, int count)
  168. {    gs_prop_item *known[3];
  169.     int code = 0;
  170.     props_extract(plist, count, props_prn, 3, known, 0);
  171.     code = gx_default_put_props(pdev, plist, count);
  172.     if ( code < 0 ) return code;
  173.     if ( known[0] != 0 )
  174.        {    gs_prop_item *pi = known[0];
  175.         if ( pi->value.i < 10000 )
  176.             pi->status = pv_rangecheck,
  177.             code = gs_error_rangecheck;
  178.         else
  179.             ppdev->use_buffer_space = known[0]->value.i;
  180.        }
  181.     if ( known[1] != 0 )
  182.         ppdev->max_bitmap = known[1]->value.i;
  183.     if ( known[2] != 0 )
  184.        {    gs_prop_item *pn = known[2];
  185.         int size = pn->value.a.size;
  186.         if ( size >= sizeof(ppdev->fname) )
  187.             pn->status = pv_limitcheck,
  188.             code = gs_error_limitcheck;
  189.         else
  190.            {    /* Close the file if it's open. */
  191.             if ( ppdev->file != NULL && ppdev->file != stdout )
  192.                 gp_close_printer(ppdev->file, ppdev->fname);
  193.             ppdev->file = NULL;
  194.             memcpy(ppdev->fname, pn->value.a.p.s, size);
  195.             ppdev->fname[size] = 0;
  196.            }
  197.        }
  198.     return code;
  199. }
  200.  
  201. /* Generic routine to send the page to the printer. */
  202. /****** Note that num_copies is currently ignored: this is wrong. ******/
  203. int
  204. gdev_prn_output_page(gx_device *pdev, int num_copies, int flush)
  205. {    int code;
  206.  
  207.     ppdev->page_count++;
  208.     code = gdev_prn_open_printer(pdev);
  209.     if ( code < 0 ) return code;
  210.  
  211.     /* print the accumulated page description */
  212.     code = (*ppdev->print_page)(ppdev, ppdev->file);
  213.     if ( code < 0 ) return code;
  214.  
  215.     code = gdev_prn_close_printer(pdev);
  216.     if ( code < 0 ) return code;
  217.  
  218.     if ( ppdev->buffer_space ) /* reinitialize clist for writing */
  219.       code = (*gs_clist_device.procs->output_page)(pdev, num_copies, flush);
  220.  
  221.     return code;
  222. }
  223.  
  224. /* Generic closing for the printer device. */
  225. /* Specific devices may wish to extend this. */
  226. int
  227. gdev_prn_close(gx_device *pdev)
  228. {    if ( ppdev->ccfile != NULL )
  229.     {    fclose(ppdev->ccfile);
  230.         fclose(ppdev->cbfile);
  231.         ppdev->ccfile = NULL;
  232.         unlink(ppdev->ccfname);
  233.         unlink(ppdev->cbfname);
  234.     }
  235.     if ( ppdev->buffer_space != 0 )
  236.     {    /* Free the buffer */
  237.         gs_free((char *)ppdev->buf, (uint)ppdev->buffer_space, 1,
  238.             "command list buffer");
  239.     }
  240.     else
  241.     {    /* Free the memory device bitmap */
  242.         gs_free(pmemdev->base, (uint)gdev_mem_bitmap_size(pmemdev),
  243.             1, "printer buffer");
  244.     }
  245.     if ( ppdev->file != NULL )
  246.     {    if ( ppdev->file != stdout )
  247.            gp_close_printer(ppdev->file, ppdev->fname);
  248.         ppdev->file = NULL;
  249.     }
  250.     pdev->procs = ppdev->orig_procs;
  251.     return 0;
  252. }
  253.  
  254. /* Map colors.  This is different from the default, because */
  255. /* printers write black, not white. */
  256.  
  257. gx_color_index
  258. gdev_prn_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  259.   gx_color_value b)
  260. {    /* Map values >= 1/2 to 0, < 1/2 to 1. */
  261.     return ((r | g | b) > gx_max_color_value / 2 ?
  262.         (gx_color_index)0 : (gx_color_index)1);
  263. }
  264.  
  265. int
  266. gdev_prn_map_color_rgb(gx_device *dev, gx_color_index color,
  267.   gx_color_value prgb[3])
  268. {    /* Map 0 to max_value, 1 to 0. */
  269.     prgb[0] = prgb[1] = prgb[2] = -((gx_color_value)color ^ 1);
  270.     return 0;
  271. }
  272.  
  273. /* ------ Driver services ------ */
  274.  
  275. /* Open the current page for printing. */
  276. int
  277. gdev_prn_open_printer(gx_device *pdev)
  278. {    char *fname = ppdev->fname;
  279.     char pfname[sizeof(ppdev->fname) + 10];
  280.     if ( strchr(fname, '%') )
  281.        {    sprintf(pfname, fname, ppdev->page_count);
  282.         fname = pfname;
  283.        }
  284.     if ( ppdev->file == NULL )
  285.        {    if ( !strcmp(fname, "-") )
  286.            ppdev->file = stdout;
  287.         else
  288.          { ppdev->file = gp_open_printer(fname);
  289.            if ( ppdev->file == NULL )
  290.                return_error(gs_error_invalidfileaccess);
  291.          }
  292.        }
  293.     return 0;
  294. }
  295.  
  296. /* Get the size of a scan line for copying. */
  297. uint
  298. gdev_prn_bytes_per_scan_line(gx_device_printer *pdev)
  299. {    return gx_device_bytes_per_scan_line((gx_device *)pdev, 0);
  300. }
  301.  
  302. /* Copy scan lines from the buffer to the printer. */
  303. int
  304. gdev_prn_get_bits(gx_device_printer *pdev, int y, byte *str, uint size, int pad)
  305. {    int swap = (*pdev->procs->get_bits)((gx_device *)pdev, y, str, size, pad);
  306.     uint line_size = gdev_prn_bytes_per_scan_line(pdev);
  307.     /* If monobit, trim off trailing garbage. */
  308.     if ( pad )
  309.        {    int extra_bits = -(pdev->width * pdev->color_info.depth) & 31;
  310.         if ( extra_bits )
  311.            {    ulong last_mask = 0xffffffffL << extra_bits;
  312.             byte *line_end = str + line_size - 4;
  313.             int n;
  314.             if ( swap )    /* swap the mask */
  315.               memswab4(&last_mask, &last_mask, sizeof(last_mask));
  316.             for ( n = size; n >= line_size;
  317.                   n -= line_size, line_end += line_size
  318.                 )
  319.                 *(ulong *)line_end &= last_mask;
  320.            }
  321.        }
  322.     else                /* know !swap */
  323.        {    byte last_mask =
  324.           0xff << (-(pdev->width * pdev->color_info.depth) & 7);
  325.         if ( last_mask != 0xff )
  326.            {    byte *line_end = str + line_size - 1;
  327.             int n;
  328.             for ( n = size; n >= line_size;
  329.                   n -= line_size, line_end += line_size
  330.                 )
  331.                 *line_end &= last_mask;
  332.            }
  333.        }
  334.     return swap;
  335. }
  336. /* Copy scan lines to a buffer.  Return the number of scan lines, */
  337. /* or <0 if error. */
  338. int
  339. gdev_prn_copy_scan_lines(gx_device_printer *pdev, int y, byte *str, uint size)
  340. {    int code;
  341.     code = gdev_prn_get_bits(pdev, y, str, size, 0);
  342.     if ( code < 0 ) return code;
  343.     return size / gdev_prn_bytes_per_scan_line(pdev);
  344. }
  345.  
  346. /* Close the current page. */
  347. int
  348. gdev_prn_close_printer(gx_device *pdev)
  349. {    if ( strchr(ppdev->fname, '%') )    /* file per page */
  350.        {    gp_close_printer(ppdev->file, ppdev->fname);
  351.         ppdev->file = NULL;
  352.        }
  353.     return 0;
  354. }
  355.